﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xyz.BusinessObjects;
using Xyz.DataObjects.Interfaces;

namespace Xyz.DataObjects.Entity.Dao
{
    public class EntityBookingDao : IBookingDao
    {
        public List<Booking> GetPaymentDueBookings(DateTime paymentDueDate, string firstName, string lastName)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                List<Booking> returnBookings = new List<Booking>();

                List<Customer> customers = new List<Customer>();
                EntityCustomerDao cDao = new EntityCustomerDao();
                var clist = cDao.GetCustomersLike(firstName, lastName) as List<Customer>;
                clist.ForEach(c => customers.Add(c));

                var bookings = new List<Booking>();
                var query = context.BookingEntities.Where(x => x.PaymentDue == paymentDueDate).ToList();

                query.ForEach(x => bookings.Add(Mapper.Map(x)));

                for (int i = bookings.Count - 1; i >= 0; i--)
                {
                    foreach(Customer c in customers)
                    {
                        if (bookings[i].CustomerId == c.CustomerId)
                        {
                            returnBookings.Add(bookings[i]);
                            break;
                        }
                    }
                }

                return returnBookings;
            }
        }

        public List<Booking> GetBookingsByCustomer(int customerId)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var bookings = new List<Booking>();
                var query = context.BookingEntities.Where(x => x.CustomerId == customerId).ToList();

                query.ForEach(x => bookings.Add(Mapper.Map(x)));

                return bookings;
            }
        }

        public Booking GetBooking(string bookingCode)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var booking = context.BookingEntities.SingleOrDefault(x => x.BookingCode == bookingCode);

                return Mapper.Map(booking);
            }
        }

        public void CreateBooking(Booking booking)
        {
            booking.BookingCode = Guid.NewGuid().ToString().GetHashCode().ToString("x");
            var entity = Mapper.Map(booking);

            using (var context = EntityObjectFactory.CreateContext())
            {
                context.BookingEntities.AddObject(entity);
                context.SaveChanges();

                booking.BookingId = entity.BookingId;
            }
        }

        public void CancelBooking(int bookingId)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var booking = context.BookingEntities.Single(x => x.BookingId == bookingId);
                booking.Status = "Cancelled";

                context.SaveChanges();
            }
        }

        public void UpdateBookingStatus(int bookingId, string status)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var booking = context.BookingEntities.Single(x => x.BookingId == bookingId);
                booking.Status = status;

                context.SaveChanges();
            }
        }
    }
}
